package com.example.agriapp.model;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.example.agriapp.ProductContract;
import com.example.agriapp.R;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

// TestDataGenerator.java
public class TestDataGenerator {
    public static List<ContentValues> generateTestProducts(Context context) {
        ContentResolver contentResolver = context.getContentResolver();

        contentResolver.delete(ProductContract.ProductEntry.CONTENT_URI, null, null);

        List<ContentValues> testProducts = new ArrayList<>();

        // 1. 红薯粉
        ContentValues product1 = new ContentValues();
        product1.put(ProductContract.ProductEntry.COLUMN_TITLE, "农家红薯粉");
        product1.put(ProductContract.ProductEntry.COLUMN_PRICE, 4.50);
        product1.put(ProductContract.ProductEntry.COLUMN_QUANTITY, 106);
        product1.put(ProductContract.ProductEntry.COLUMN_CATEGORY, "粮食");
        product1.put(ProductContract.ProductEntry.COLUMN_IMAGE,
                getImageBytes(context, R.drawable.sweet_potato_noodles));
        testProducts.add(product1);

        // 2. 土蜂蜜
        ContentValues product2 = new ContentValues();
        product2.put(ProductContract.ProductEntry.COLUMN_TITLE, "野生纯蜂蜜");
        product2.put(ProductContract.ProductEntry.COLUMN_PRICE, 45.00);
        product2.put(ProductContract.ProductEntry.COLUMN_QUANTITY, 792);
        product2.put(ProductContract.ProductEntry.COLUMN_CATEGORY, "特产");
        product2.put(ProductContract.ProductEntry.COLUMN_IMAGE,
                getImageBytes(context, R.drawable.honey));
        testProducts.add(product2);

        // 3. 有机大米
        ContentValues product3 = new ContentValues();
        product3.put(ProductContract.ProductEntry.COLUMN_TITLE, "有机五常大米");
        product3.put(ProductContract.ProductEntry.COLUMN_PRICE, 8.80);
        product3.put(ProductContract.ProductEntry.COLUMN_QUANTITY, 342);
        product3.put(ProductContract.ProductEntry.COLUMN_CATEGORY, "粮食");
        product3.put(ProductContract.ProductEntry.COLUMN_IMAGE,
                getImageBytes(context, R.drawable.rice));
        testProducts.add(product3);

        // 4. 新鲜草莓
        ContentValues product4 = new ContentValues();
        product4.put(ProductContract.ProductEntry.COLUMN_TITLE, "新鲜草莓 500g");
        product4.put(ProductContract.ProductEntry.COLUMN_PRICE, 15.90);
        product4.put(ProductContract.ProductEntry.COLUMN_QUANTITY, 56);
        product4.put(ProductContract.ProductEntry.COLUMN_CATEGORY, "水果");
        product4.put(ProductContract.ProductEntry.COLUMN_IMAGE,
                getImageBytes(context, R.drawable.strawberry));
        testProducts.add(product4);

        // 5. 农家土鸡蛋
        ContentValues product5 = new ContentValues();
        product5.put(ProductContract.ProductEntry.COLUMN_TITLE, "农家土鸡蛋 30枚");
        product5.put(ProductContract.ProductEntry.COLUMN_PRICE, 25.00);
        product5.put(ProductContract.ProductEntry.COLUMN_QUANTITY, 128);
        product5.put(ProductContract.ProductEntry.COLUMN_CATEGORY, "禽蛋");
        product5.put(ProductContract.ProductEntry.COLUMN_IMAGE,
                getImageBytes(context, R.drawable.eggs));
        testProducts.add(product5);

        return testProducts;
    }

    private static byte[] getImageBytes(Context context, int resId) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }
}